package com.central.apps.controller;

import com.central.app.service.HandlerService;
import io.swagger.annotations.Api;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

@RestController
@Api(tags = "库存单据联查")
@RequestMapping("/inocuassquery")
public class inocuassqueryController {
    @Autowired
    private HandlerService handlerService;
    @PostMapping("/inocuassquery")
    public Map<String,Object> inocuassquery (@RequestBody Map<String,Object> params) throws Exception{
        Integer page = MapUtils.getInteger(params, "page");
        Integer limit = MapUtils.getInteger(params, "limit");
        String itemnum = MapUtils.getString(params, "itemnum");
        String batchnum = MapUtils.getString(params, "batchnum");
        String itemdesc = MapUtils.getString(params, "itemdesc");
        ArrayList<String> strings = new ArrayList<>();
        strings.add(itemnum);
        strings.add("itemnum");
        strings.add(itemdesc);
        strings.add("itemdesc");
        strings.add(batchnum);
        strings.add("batchnum");
        HashMap<String, Object> hashMap1 = new HashMap<>();
        List<Map<String, Object>> bySql=null;
        List<Map<String, Object>> bySql1=null;
        if (itemnum==null&&batchnum==null&&itemdesc==null){
            //采购接收
            bySql = handlerService.findBySql("SELECT\n" +
                    "DISTINCT\n" +
                    "st.storedesc,\n" +
                    "t.uuidprice as id,\n" +
                    "t.id as rowid,\n" +
                    "t.batchnum,\n" +
                    "\tt.itemnum,\n" +
                    "\tt.itemdesc,\n" +
                    "\tt.model,\n" +
                    "\tb.instonum as ruinstonum,\n" +
                    "\tc.instonum as outinstonum,\n" +
                    "\ta.receiptnum,\n" +
                    "\td.glnum,\n" +
                    "\tf.transferstonum as transferstonum1,\n" +
                    "\th.transferstonum as transferstonum2,\n" +
//                    "\tj.Pid,\n" +
                    "\tk.scraption\n" +
                    "\tfrom gq_prtdetailline t\n" +
                    "\tLEFT JOIN gq_store st ON t.warehousenum = st.storenum \n" +
                    "\tLEFT JOIN gq_purchasereceipt a ON t.prtid = a.id \n" +
                    "\tleft join gq_itemapplyline d on t.batchnum=d.lotnum\n" +
                    "\tleft join gq_transferstoitem e on e.menutype=2 and t.batchnum=e.fromlot\n" +
                    "\tleft join gq_transfersto f on f.menutype=2 and e.transferstoid=f.id\n" +
                    "\tleft join gq_transferstoitem g on g.menutype=1 and t.batchnum=g.fromlot\n" +
                    "\tleft join gq_transfersto h on h.menutype=1 and g.transferstoid=h.id\n" +
//                    "\tleft join gq_check j on j.id=i.pidd \n" +
                    "\tleft join gq_Listscrapped k  on k.batch=t.batchnum\n" +
                    "\tleft join gq_matusetrans b on b.type='接收' and (t.batchnum = b.lotnum or b.lotnum=e.fromlot) \t\n" +
                    "left join gq_matusetrans c on c.type in('领用出库','报废出库','销售出库','返厂维修') and t.batchnum=c.lotnum and (c.receiptnum=d.glnum or c.receiptnum=f.transferstonum or c.receiptnum=k.scraption)");
            //入库申请
            bySql1  = handlerService.findBySql("SELECT\n" +
                    "DISTINCT\n" +
                    "st.storedesc,\n" +
                    "t.uuidprice as id,\n" +
                    "t.id as rowid,\n" +
                    "t.batchnum,\n" +
                    "\tt.itemnum,\n" +
                    "\tt.itemdesc,\n" +
                    "\tt.model,\n" +
                    "\ta.instonum as ruinstonum,\n" +
                    "\tj.instonum as outinstonum,\n" +
                    "\tt.instonum,\n" +
                    "\tb.glnum,\n" +
                    "\td.transferstonum as transferstonum1,\n" +
                    "\tf.transferstonum as transferstonum2,\n" +
//                    "\th.Pid,\n" +
                    "\ti.scraption\n" +
                    "FROM\n" +
                    "\tgq_instoline t\n" +
                    "\tLEFT JOIN gq_store st ON t.storageroom = st.storenum \n" +
                    "\tleft join gq_itemapplyline b on t.batchnum=b.lotnum\n" +
                    "\tleft join gq_transferstoitem c on c.menutype=2 and t.batchnum=c.fromlot\n" +
                    "\tleft join gq_transfersto d on d.menutype=2 and c.transferstoid=d.id\n" +
                    "\tleft join gq_transferstoitem e on e.menutype=1 and t.batchnum=e.fromlot\n" +
                    "\tleft join gq_transfersto f on f.menutype=1 and e.transferstoid=f.id\n" +
//                    "\tleft join gq_check h on h.id=g.pidd \n" +
                    "\tleft join gq_Listscrapped i  on i.batch=t.batchnum\n" +
                    "\tLEFT JOIN gq_matusetrans a ON a.type='接收' and (t.batchnum=a.lotnum or  a.lotnum=c.fromlot)\n" +
                    "\tleft join gq_matusetrans j on j.type in('领用出库','报废出库','销售出库','返厂维修') and t.batchnum=j.lotnum and (j.receiptnum=b.glnum or j.receiptnum=d.transferstonum or j.receiptnum=i.scraption) ");
        }else {
            String list = " t.";
            String list1=" like";
            String list3="";
            //String list2="and";
            for (int i=0;i<strings.size();i=i+2){
                String s = strings.get(i);
                if (s!=null){
                    if(list3==""){
                        list3=list3+list+strings.get(i+1)+list1+"'%"+s+"%'";
                    }else{
                        list3=list3+" and "+list+strings.get(i+1)+list1+"'%"+s+"%'";
                    }
                }
            }
            //采购接收
            bySql = handlerService.findBySql("SELECT\n" +
                    "DISTINCT\n" +
                    "st.storedesc,\n" +
                    "t.uuidprice as id,\n" +
                    "t.id as rowid,\n" +
                    "t.batchnum,\n" +
                    "\tt.itemnum,\n" +
                    "\tt.itemdesc,\n" +
                    "\tt.model,\n" +
                    "\tb.instonum as ruinstonum,\n" +
                    "\tc.instonum as outinstonum,\n" +
                    "\ta.receiptnum,\n" +
                    "\td.glnum,\n" +
                    "\tf.transferstonum as transferstonum1,\n" +
                    "\th.transferstonum as transferstonum2,\n" +
//                    "\tj.Pid,\n" +
                    "\tk.scraption\n" +
                    "\tfrom gq_prtdetailline t\n" +
                    "\tLEFT JOIN gq_store st ON t.warehousenum = st.storenum \n" +
                    "\tLEFT JOIN gq_purchasereceipt a ON t.prtid = a.id \n" +
                    "\tleft join gq_itemapplyline d on t.batchnum=d.lotnum\n" +
                    "\tleft join gq_transferstoitem e on e.menutype=2 and t.batchnum=e.fromlot\n" +
                    "\tleft join gq_transfersto f on f.menutype=2 and e.transferstoid=f.id\n" +
                    "\tleft join gq_transferstoitem g on g.menutype=1 and t.batchnum=g.fromlot\n" +
                    "\tleft join gq_transfersto h on h.menutype=1 and g.transferstoid=h.id\n" +
//                    "\tleft join gq_check j on j.id=i.pidd \n" +
                    "\tleft join gq_Listscrapped k  on k.batch=t.batchnum\n" +
                    "\tleft join gq_matusetrans b on b.type='接收' and (t.batchnum = b.lotnum or b.lotnum=e.fromlot) \t\n" +
                    "left join gq_matusetrans c on c.type in('领用出库','报废出库','销售出库','返厂维修') and t.batchnum=c.lotnum and (c.receiptnum=d.glnum or c.receiptnum=f.transferstonum or c.receiptnum=k.scraption) where "+list3+"");
            //入库申请
            bySql1  = handlerService.findBySql("SELECT\n" +
                    "DISTINCT\n" +
                    "st.storedesc,\n" +
                    "t.uuidprice as id,\n" +
                    "t.id as rowid,\n" +
                    "t.batchnum,\n" +
                    "\tt.itemnum,\n" +
                    "\tt.itemdesc,\n" +
                    "\tt.model,\n" +
                    "\ta.instonum as ruinstonum,\n" +
                    "\tj.instonum as outinstonum,\n" +
                    "\tt.instonum,\n" +
                    "\tb.glnum,\n" +
                    "\td.transferstonum as transferstonum1,\n" +
                    "\tf.transferstonum as transferstonum2,\n" +
//                    "\th.Pid,\n" +
                    "\ti.scraption\n" +
                    "FROM\n" +
                    "\tgq_instoline t\n" +
                    "\tLEFT JOIN gq_store st ON t.storageroom = st.storenum \n" +
                    "\tleft join gq_itemapplyline b on t.batchnum=b.lotnum\n" +
                    "\tleft join gq_transferstoitem c on c.menutype=2 and t.batchnum=c.fromlot\n" +
                    "\tleft join gq_transfersto d on d.menutype=2 and c.transferstoid=d.id\n" +
                    "\tleft join gq_transferstoitem e on e.menutype=1 and t.batchnum=e.fromlot\n" +
                    "\tleft join gq_transfersto f on f.menutype=1 and e.transferstoid=f.id\n" +
//                    "\tleft join gq_check h on h.id=g.pidd \n" +
                    "\tleft join gq_Listscrapped i  on i.batch=t.batchnum\n" +
                    "\tLEFT JOIN gq_matusetrans a ON a.type='接收' and (t.batchnum=a.lotnum or  a.lotnum=c.fromlot)\n" +
                    "\tleft join gq_matusetrans j on j.type in('领用出库','报废出库','销售出库','返厂维修') and t.batchnum=j.lotnum and (j.receiptnum=b.glnum or j.receiptnum=d.transferstonum or j.receiptnum=i.scraption) where "+list3+"");
        }

        bySql.addAll(bySql1);

        List<Map<String,Object>> collect = bySql.stream().skip((page-1)*limit).limit(limit).collect(Collectors.toList());

        // 重新赋值id值
        for (Map<String,Object> map : collect) {
            map.put("id", UUID.randomUUID());
        }

        String code="0";
        hashMap1.put("count",bySql.size());
        hashMap1.put("code",code);
        hashMap1.put("data",collect);

        return hashMap1;
    }
    //选择弹出框
    @PostMapping("/chinocuassquery")
    public Map<String,Object> chinocuassquery (@RequestBody Map<String,Object> params) throws Exception{
        String batchnum = MapUtils.getString(params, "batchnum");
        Integer page = MapUtils.getInteger(params, "page");
        Integer limit = MapUtils.getInteger(params, "limit");
        HashMap<String, Object> hashMap1 = new HashMap<>();

        //未传批次，应该不显示数据
        if(StringUtils.isBlank(batchnum)){
            hashMap1.put("count",0);
            hashMap1.put("code",0);
            hashMap1.put("data",null);
            return hashMap1;
        }
        //采购接收
        List<Map<String, Object>> bySql = handlerService.findBySql("SELECT\n" +
                "                                    DISTINCT\n" +
                "                                    t.id,\n" +
                "                                    t.batchnum,\n" +
                "                                    t.itemnum,\n" +
                "                                    t.itemdesc,\n" +
                "                                    t.model,\n" +
                "                                    b.instonum as ruinstonum,\n" +
                "                                    c.instonum as outinstonum,\n" +
                "                                    a.receiptnum,\n" +
                "                                    d.glnum,\n" +
                "                                    f.transferstonum as transferstonum1,\n" +
                "                                    h.transferstonum as transferstonum2,\n" +
                "                                    k.scraption\n" +
                "                                    from gq_prtdetailline t\n" +
                "                                    LEFT JOIN gq_purchasereceipt a ON t.prtid = a.id \n" +
                "                                   left join gq_itemapplyline d on t.batchnum=d.lotnum\n" +
                "                                    left join gq_transferstoitem e on e.menutype=2 and t.batchnum=e.fromlot\n" +
                "                                    left join gq_transfersto f on f.menutype=2 and e.transferstoid=f.id\n" +
                "                                    left join gq_transferstoitem g on g.menutype=1 and t.batchnum=g.fromlot\n" +
                "                                    left join gq_transfersto h on h.menutype=1 and g.transferstoid=h.id\n" +
                "                                    left join gq_Listscrapped k  on k.batch=t.batchnum\n" +
                "                                    left join gq_matusetrans b on b.type='接收' and (t.batchnum = b.lotnum or b.lotnum=e.fromlot) \n" +
                "                                   left join gq_matusetrans c on c.type in('领用出库','报废出库','销售出库','返厂维修') and t.batchnum=c.lotnum and (c.receiptnum=d.glnum or c.receiptnum=f.transferstonum or c.receiptnum=k.scraption) where t.batchnum='" + batchnum + "'");
        //入库申请
        List<Map<String, Object>>  bySql1  = handlerService.findBySql("SELECT\n" +
                "DISTINCT\n" +
                "\tt.id,\n" +
                "t.batchnum,\n" +
                "\tt.itemnum,\n" +
                "\tt.itemdesc,\n" +
                "\tt.model,\n" +
                "\ta.instonum as ruinstonum,\n" +
                "\tj.instonum as outinstonum,\n" +
                "\tt.instonum,\n" +
                "\tb.glnum,\n" +
                "\td.transferstonum as transferstonum1,\n" +
                "\tf.transferstonum as transferstonum2,\n" +
                "\ti.scraption\n" +
                "FROM\n" +
                "\tgq_instoline t\n" +
                "\tleft join gq_itemapplyline b on t.batchnum=b.lotnum\n" +
                "\tleft join gq_transferstoitem c on c.menutype=2 and t.batchnum=c.fromlot\n" +
                "\tleft join gq_transfersto d on d.menutype=2 and c.transferstoid=d.id\n" +
                "\tleft join gq_transferstoitem e on e.menutype=1 and t.batchnum=e.fromlot\n" +
                "\tleft join gq_transfersto f on f.menutype=1 and e.transferstoid=f.id\n" +
                "\tleft join gq_Listscrapped i  on i.batch=t.batchnum\n" +
                "\tLEFT JOIN gq_matusetrans a ON a.type='接收' and (t.batchnum=a.lotnum or  a.lotnum=c.fromlot)\n" +
                "\tleft join gq_matusetrans j on j.type in('领用出库','报废出库','销售出库','返厂维修') and t.batchnum=j.lotnum and (j.receiptnum=b.glnum or j.receiptnum=d.transferstonum or j.receiptnum=i.scraption) where t.batchnum='" + batchnum + "'");
        bySql.addAll(bySql1);

        List<Map<String,Object>> collect = bySql.stream().skip((page-1)*limit).limit(limit).collect(Collectors.toList());
        String code="0";
        hashMap1.put("count",bySql.size());
        hashMap1.put("code",code);
        hashMap1.put("data",collect);

        return hashMap1;
    }

}
